package com.sgb.knowledgeshare.view.fragment;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.swgk.core.view.entity.ChannelEntity;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.FragmentShareClassifyBinding;
import com.swgk.core.view.entity.VideoTypeListEntity;
import com.sgb.knowledgeshare.viewmodel.ShareClassifyModel;
import com.sgb.knowledgeshare.weight.ShareTitlePopWindow;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import java.util.ArrayList;
import java.util.List;

/**
 * 分享-分类
 */
public class ShareClassifyFragment extends AppLazyFragment {

    ShareClassifyModel viewmodel;
    private FragmentShareClassifyBinding binding;
    private ArrayList<Fragment> mFragments;
    private FragPagerAdapter pagerAdapter;
    private ShareTitlePopWindow titlePopWindow;
    List<ChannelEntity> channelEntityList = new ArrayList<>();
    private List<VideoTypeListEntity> dictdata = new ArrayList<>();
    private String[] titles;
    private Gson gson;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_share_classify, container, false);
        viewmodel = new ViewModelProvider(this).get(ShareClassifyModel.class);
        viewmodel.setActivity(ShareClassifyFragment.this);
        View decor = getActivity().getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding.classifySort.setOnClickListener(this);
        binding.viewpager.setNoScroll(true);
        binding.setViewModel(viewmodel);
        return binding.getRoot();
    }

    @Override
    protected void onLazyLoad() {
        initdata();
    }

    @Override
    public void onVisible() {
        super.onVisible();
        if (mFragments != null && mFragments.size() > 0) {
            if (null!=binding.tablayout){
                binding.tablayout.setCurrentTab(0);
            }
        }
        initdata();
    }

    private void initdata() {
        viewmodel.getvideoTypeList();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.classify_sort) {//排序弹窗
            ShareClassifyPop();
        }
    }

    private void ShareClassifyPop() {
        if (titlePopWindow != null)
            titlePopWindow.showPopwindowshare(binding.viewHead);
    }

    //    获取类型列表数据
    public void setVideoTypeList(List<VideoTypeListEntity> data) {
        gson = new Gson();

        if (null != data && data.size() > 0) {
            String stringData = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.SHARE_CHANNEL_DATA, "");
            dictdata.clear();
            if (!TextUtils.isEmpty(stringData)) {
                channelEntityList = gson.fromJson(stringData, new TypeToken<List<ChannelEntity>>() {
                }.getType());
                for (ChannelEntity channelEntity : channelEntityList) {
                    VideoTypeListEntity videoTypeListEntity = new VideoTypeListEntity();
                    videoTypeListEntity.setName(channelEntity.getName());
                    videoTypeListEntity.setNo(channelEntity.getSort());
                    videoTypeListEntity.setChildren(channelEntity.getChildren());
                    dictdata.add(videoTypeListEntity);
                }
            } else {
                dictdata.addAll(data);
                for (VideoTypeListEntity videotype : dictdata) {
                    ChannelEntity channelEntity = new ChannelEntity();
                        channelEntity.setName(videotype.getName());
                        channelEntity.setSort(videotype.getNo());
                        channelEntity.setChildren(videotype.getChildren());
                        channelEntityList.add(channelEntity);
                }
            }
            mFragments = new ArrayList<>();
            int size = dictdata.size();
            titles = new String[size];
            for (int i = 0; i < size; i++) {
                mFragments.add(ShareClassifyListFragment.newInstance(dictdata.get(i).getNo(),dictdata.get(i).getChildren()));
                titles[i] = dictdata.get(i).getName();
            }
            pagerAdapter = new FragPagerAdapter(getChildFragmentManager(), mFragments);
            binding.viewpager.setAdapter(pagerAdapter);
            binding.tablayout.setViewPager(binding.viewpager, titles);
        } else {
            MToast.showToast(getActivity(), "数据未加载");
        }
        titlePopWindow = new ShareTitlePopWindow(getActivity(), channelEntityList);
        titlePopWindow.setBgView(binding.grayLayout);
        titlePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                //存入本地
                channelEntityList = (List<ChannelEntity>) key1;
                String channelData = gson.toJson(channelEntityList);
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.SHARE_CHANNEL_DATA, channelData);
                mFragments.clear();
                titles = null;
                titles = new String[channelEntityList.size()];
                for (int i = 0; i < channelEntityList.size(); i++) {
                    mFragments.add(ShareClassifyListFragment.newInstance(channelEntityList.get(i).getSort(),channelEntityList.get(i).getChildren()));
                    titles[i] = channelEntityList.get(i).getName();
                }
                pagerAdapter = new FragPagerAdapter(getChildFragmentManager(), mFragments);
                binding.viewpager.setAdapter(pagerAdapter);
                binding.viewpager.setCurrentItem(0);
                binding.tablayout.setCurrentTab(0);
                binding.tablayout.setViewPager(binding.viewpager, titles);
            }
        });
    }
}
